// Serverside

HG.RESOURCE = {
	["materials"] = { },
	["particles"] = { },
	["models"] = { },
	["fonts"] = {},
	["scripts"] = {}
}

resource.AddFile("materials/trails/hgtrail_rf.vmt")
resource.AddFile("materials/trails/hgtrail_logo.vmt")
resource.AddFile("materials/trails/hgtrail_hexagon.vmt")
resource.AddFile("materials/trails/hgtrail_binary.vmt")
resource.AddFile("materials/trails/hgtrail_hg.vmt")
resource.AddFile("materials/trails/hgtrail_rainbow.vmt")
resource.AddFile("materials/VGUI/hg_logo1.vmt")
resource.AddFile("particles/particles_manifest.txt")
resource.AddFile("particles/gmod_player_auras.pcf")

HG.PLAYERS = hgframe.GetAllPlayers() or {}

HG.GLOBALS = {
	NoticeMessage = "If you experience bugs/errors, you can report them through the 'Help' tab below (Contact Us)"
}
--------------------------------------
local meta = FindMetaTable("Player")
if not meta then return end

function meta:hg_InitClientData()
	umsg.Start( "HGF_InitData", self )
		print( "HG Initializing Client Datatable" )
	umsg.End()
end

function meta:hg_UpdateClientData(data)
	if !data then data = self:HG_DataTable() end
	umsg.Start( "HGF_UpdateData", self )
		print( "HG Updating Client Datatable" )
		umsg.Short(data.hgid)
		umsg.String(data.steamid)
		umsg.Short(data.uniqueid)
		umsg.Short(data.money)
	umsg.End()
	PrintTable(data)
end

function meta:hg_UpdateClientItems(items)
	local items = items or self:HG_GetItems()
	local nw_items = {}
	for k, itemid in ipairs(items) do
		nw_items[k] = HG.ItemID(itemid)
	end
	umsg.Start( "HGF_UpdateItems", self )
		print( "HG Updating Client Items" )
		umsg.Short(#items)
		umsg.String(table.concat(nw_items,"|"))
	umsg.End()
	PrintTable(nw_items)
end

function meta:hg_UpdateMoney(amount)
	umsg.Start( "HGF_ModMoney", self )
		umsg.Short(amount)
	umsg.End()
end

function meta:hg_UpdateItem(itemid)
	print( "HG Giving item "..itemid.." to player "..tostring(self) )
	umsg.Start( "HGF_AddItem", self )
		umsg.String(itemid)
	umsg.End()
end

function meta:hg_EquipItem(itemid)
	if !self:HG_HasItem(itemid) then return; end
	local slot = string.lower( HG.SLOTS[ HG.ItemSlot(itemid) ].name )
	if slot == "hat" then
		self:hg_EquipHat(itemid)
	elseif slot == "trail" then
		self:hg_EquipTrail(itemid)
	elseif slot == "aura" then
		self:hg_EquipAura(itemid)
	end
end

	function meta:hg_EquipHat(itemid)
		umsg.Start("hgf_EquipHat")
			umsg.Entity(self)
			umsg.String(itemid)
		umsg.End()
	end

	function meta:hg_EquipAura(itemid)
		umsg.Start("hgf_EquipAura")
			umsg.Entity(self)
			umsg.String(itemid)
		umsg.End()
	end

	function meta:hg_EquipTrail(itemid)
		local convar = self:GetInfo("hg_trail")
		if convar == itemid or itemid == "" then
			if self._Trail then
				SafeRemoveEntity(self._Trail)
				self._Trail = nil
				print( "UNEQUIPING TRAIL: "..itemid )
			end
		else
		
			if self._Trail then
				SafeRemoveEntity(self._Trail)
				self._Trail = nil
				--print( "UNEQUIPING TRAIL: "..itemid )
			end
			
			local item = HG.Item(itemid)
			local slot = HG.ItemSlot(itemid)
			local bone = self:LookupBone( "ValveBiped.Bip01_Pelvis" )
			
			self._Trail = ents.Create( "info_target" )
			self._Trail:SetParent(self)
			self._Trail:SetPos(self:GetBonePosition(bone))
			--self._Trail:Fire("setparentattachmentmaintainoffset", "hips")
			util.SpriteTrail(self._Trail, 
				0,
				item.Color,
				item.Additive,
				item.StartWidth,
				item.EndWidth,
				item.Lifetime,
				1/((item.StartWidth+item.EndWidth) *0.5), 
				item.TrailTexture )
			print( "EQUIPING TRAIL: "..itemid )
		end
	end
	
--------------------------------------
hook.Add("PlayerAuthed","hgf_PlayerAuthedSV", function( pl, stid, unid )
	print( "HG Authorizing player:" ..tostring(pl).." SteamID: "..stid.." UniqueID: "..unid )
	pl:hg_InitClientData()
	pl:HG_JoinedBefore()
end)

hook.Add("PlayerInitialSpawn","hgf_PlayerInitSpawnSV", function( pl )
	timer.Simple(1, function()
		print( "HG InitialSpawn for "..tostring(pl).." debugging data...")
		local data, items = pl:HG_DataTable(), pl:HG_GetItems()
		if data and items then 
			pl:hg_UpdateClientData(data)
			pl:hg_UpdateClientItems(items)
		end
	end)
end)

hook.Add("OnNPCKilled", "hgf_NPCKilled", function( npc, killer, weapon )
	if IsValid(killer) and killer:IsPlayer() then
		killer:HG_ModMoney(math.random(100,250))
	end
end )

---------------------------------------
concommand.Add( "hg_item_cmd", function( pl, cmd, arg )
	
	if not pl or not IsValid(pl) then return end
	
	local cmd, itemid
	
	cmd 		= arg[1] or "buy"
	itemid 		= arg[2] or "hat_bluehardhat"
	
	if cmd == "buy"then
	
		local price = tonumber(HG.ItemBPrice(itemid))
		pl:HG_BuyItem(itemid, price)
	
	elseif cmd == "equip" then
	
		pl:hg_EquipItem(itemid)
	
	end
	
end)

function HG.GlobalNoticeMessage( text )

	for k, v in ipairs(player.GetAll()) do
		hgframe.NotifyPlayer(v,text)
	end

end